
package rentalgui;

import database.PropertyDao;
import database.ProviderDao;
import database.ServiceDao;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import rentaldb.Property;
import rentaldb.Provider;
import rentaldb.Service;

public class ServiceUpdateDeleteWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseUpdateDeleteWindow
     */
    public ServiceUpdateDeleteWindow(PropertyDao pdao, ServiceDao sdao, ProviderDao provdao) {
        initComponents();
        updateMode = false;
        this.pdao = pdao;
        this.sdao = sdao;
        this.provdao = provdao;      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        inputBox = new javax.swing.JTextField();
        clearButton = new javax.swing.JButton();
        inputServiceId = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        inputType = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        inputCost = new javax.swing.JTextField();
        inputDateOfService = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        inputProviderId = new javax.swing.JTextField();
        inputPropertyId = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Service Update/Delete");

        jLabel7.setText("Service ID");

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.setEnabled(false);
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        updateButton.setText("Update");
        updateButton.setEnabled(false);
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        inputServiceId.setEditable(false);

        jLabel1.setText("Service ID");

        inputType.setEditable(false);

        jLabel2.setText("Date of Service");

        inputCost.setEditable(false);

        inputDateOfService.setEditable(false);

        jLabel3.setText("Type");

        jLabel8.setText("Provider ID");

        inputProviderId.setEditable(false);

        inputPropertyId.setEditable(false);

        jLabel10.setText("Property ID");

        jLabel4.setText("Cost");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputBox)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputProviderId, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputPropertyId, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputServiceId)
                            .addComponent(inputDateOfService, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputType)
                            .addComponent(inputCost))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(inputBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(clearButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(inputServiceId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputDateOfService, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(inputType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(inputCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputPropertyId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputProviderId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exitButton)
                    .addComponent(updateButton)
                    .addComponent(deleteButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        clearTextBoxes();
        setEditableStatus(false);
        updateMode = false;
        updateButton.setEnabled(false);
        deleteButton.setEnabled(false);
    }//GEN-LAST:event_clearButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try{
            Integer input = Integer.parseInt(inputBox.getText().trim());
            curService = sdao.getServiceById(input);
            putCurrentInTextBoxes();
            setEditableStatus(true);
            updateMode = false;
            updateButton.setEnabled(true);
            deleteButton.setEnabled(true);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Invalid ID.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }        
    }//GEN-LAST:event_searchButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this entry?", "Delete?", JOptionPane.YES_NO_OPTION);
        if(input == JOptionPane.YES_OPTION){
            try{
                sdao.delete(curService);
                clearButtonActionPerformed(evt);
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, "Error during delete.", "Delete Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        if(!updateMode){
            updateMode = true;
            JOptionPane.showMessageDialog(null, "Make the desired changes to the fields, then click update again.", "Update Mode", JOptionPane.INFORMATION_MESSAGE);
            deleteButton.setEnabled(false);
            setEditableStatus(true);
        }else{
            updateMode = false;
            deleteButton.setEnabled(true);
            int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to update this entry?", "Update?", JOptionPane.YES_NO_OPTION);
            if(input == JOptionPane.YES_OPTION){
                try{
                    Integer serviceId = Integer.parseInt(inputServiceId.getText().trim());
                    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                    Date dateOfService = df.parse(inputDateOfService.getText().trim());
                    String type = inputType.getText().trim();                
                    Double cost = Double.parseDouble(inputCost.getText().trim());
                    Integer propertyId = Integer.parseInt(inputPropertyId.getText().trim());
                    Property property = pdao.getPropertyById(propertyId);
                    Integer providerId = Integer.parseInt(inputProviderId.getText().trim());
                    Provider provider = provdao.getProviderById(providerId);

                    Service service = new Service();
                    service.setServiceId(serviceId);
                    service.setDateofservice(dateOfService);
                    service.setType(type);
                    service.setCost(cost);
                    service.setPropertyid(property);
                    service.setProviderid(provider);
                    sdao.update(service);
                }
                catch(Exception e){
                    JOptionPane.showMessageDialog(null, "Error during update.", "Update Error", JOptionPane.ERROR_MESSAGE);
                }
            } else{
                JOptionPane.showMessageDialog(this, "Changes Reset", "Update Cancelled", JOptionPane.INFORMATION_MESSAGE);
                putCurrentInTextBoxes();
            }
            deleteButton.setEnabled(false);
            setEditableStatus(false);
        }
    }//GEN-LAST:event_updateButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        dispose();
    }//GEN-LAST:event_exitButtonActionPerformed

    private void setEditableStatus(boolean setting){
        inputBox.setEditable(setting);
        inputDateOfService.setEditable(setting);
        inputType.setEditable(setting);
        inputCost.setEditable(setting);
        inputPropertyId.setEditable(setting);
        inputProviderId.setEditable(setting);
    }
    
    private void clearTextBoxes(){
        inputBox.setText("");
        inputServiceId.setText("");
        inputDateOfService.setText("");
        inputType.setText("");
        inputCost.setText("");
        inputPropertyId.setText("");
        inputProviderId.setText("");
    }
    
    private void putCurrentInTextBoxes(){
        inputServiceId.setText(curService.getServiceId().toString().trim());
        inputDateOfService.setText(curService.getDateofservice().toString().trim());
        inputType.setText(curService.getType().toString().trim());
        inputCost.setText(curService.getCost().toString().trim());
        inputPropertyId.setText(curService.getPropertyid().toString().trim());
        inputProviderId.setText(curService.getProviderid().toString().trim());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JTextField inputBox;
    private javax.swing.JTextField inputCost;
    private javax.swing.JTextField inputDateOfService;
    private javax.swing.JTextField inputPropertyId;
    private javax.swing.JTextField inputProviderId;
    private javax.swing.JTextField inputServiceId;
    private javax.swing.JTextField inputType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
    private Service curService;
    private PropertyDao pdao;
    private ServiceDao sdao;
    private ProviderDao provdao;
    private boolean updateMode;
}
